using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Services;
using Dimok.Aspx.Manager.Lib;
using Dimok.Credit;
using Dimok.Data;
using Dimok.Exceptions;
using Data;

namespace Dimok.Aspx.Manager.Application.WS{
	[WebService(Namespace="http://Air.com/application/")]
	public class Application : Dimok.Aspx.Manager.Lib.WS{
		[WebMethod]
		public string GuessLastName(string lastName){
			try{
				return SQL.XmlInnerText(SQL.ExecuteDataset(UOX.dbRaw,"s_GuessLastName",lastName));
			}catch(Exception exc){throw new ClientException("\nLastName:"+lastName,exc,null);}
		}
		[WebMethod]
		public int GetApplIDBySSN(string SSN){
			try{
				throw new Exception("Method GetApplIDBySSN must be re-build.");
				object applID = SQL.ExecuteScalarEx(uox.db,"SELECT ApplicantID FROM t_Applicants WHERE MenuID = 59 AND SSN=@SSN",SSN);
				return (int)applID;
			}catch(Exception exc){throw new ClientException(exc);}
		}
		[WebMethod]
		public void ShowApplPages(int folderID){
			try{//------------------------------------------
				UOX uox = new UOX();
				byte[] image = uox.ImageFolder(folderID);
				if( image != null && image.Length != 0 ){
					Context.Response.ContentType = "image/tiff";
					Context.Response.BinaryWrite(image);
				}else{
					object folderName = SQL.ExecuteScalar(uox.db,CommandType.Text,"SELECT ImageType FROM t_ImageType WHERE ID="+folderID);
					Context.Response.Write("<pre>No images in \""+folderName+"\".</pre>");
				}
			}catch(Exception exp){//----------------------
				Context.Response.ContentType = "text/html";
				Context.Response.Write("<pre>"+exp.Message+"</pre>");
			}//-------------------------------------------
		}
		[WebMethod]
		public void RemoveCoApplicant(){
			try{
				if( UOX.HasPageSessionID ) uox.LockApplicant();
				uox.RemoveCoApplicant();
			}catch(Exception exc){throw new ClientException(exc);}
		}
		[WebMethod]
		public string GetCitiesByZip(string zipCode,int index){
			try{
				SQL.GetCitiesByZip(uox.db,zipCode,uox.ApplInfo.BorrowerID(0),index);
				return SQL.GetCitiesByZip(uox.dbADO,zipCode);
			}catch(Exception exp){ throw new ClientException(exp);}
		}
		[WebMethod]
		public string GetStateByZip(string zipCode){
			try{
				SQL.ExecSP(uox.db,"s_SetCityStateByZip",uox.ApplInfo.BorrowerID(0),0,zipCode);
				object state = SQL.ExecuteScalarEx(uox.db,"SELECT StateCode FROM t_Address WHERE BorrowerID=@BID AND Pos=@Index",uox.ApplInfo.BorrowerID(0),0);
				if( state == System.DBNull.Value )throw new ClientException("Zip code doesn't have a state.");
				return (string)state;
			}catch(Exception exc){ throw new ClientException(exc); }
		}

		#region Employment Info
		[WebMethod]
		public void xmlEmplInfo_0(string fieldName,string fieldValue){
			try{
				if( UOX.HasPageSessionID ) uox.LockApplicant();
				xmlEmplInfo(0,0,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException("Field:"+fieldName+",Value:"+fieldValue+"\n",exp);}
		}
		[WebMethod]
		public void xmlEmplInfo_1(string fieldName,string fieldValue){
			try{
				if( UOX.HasPageSessionID ) uox.LockApplicant();
				xmlEmplInfo(0,1,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException("Field:"+fieldName+",Value:"+fieldValue+"\n",exp);}
		}
		[WebMethod]
		public void xmlEmplInfoCo_0(string fieldName,string fieldValue){
			try{
				if( UOX.HasPageSessionID ) uox.LockApplicant();
				xmlEmplInfo(1,0,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException("Field:"+fieldName+",Value:"+fieldValue+"\n",exp);}
		}
		[WebMethod]
		public void xmlEmplInfoCo_1(string fieldName,string fieldValue){
			try{
				if( UOX.HasPageSessionID ) uox.LockApplicant();
				xmlEmplInfo(1,1,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException("Field:"+fieldName+",Value:"+fieldValue+"\n",exp);}
		}
		[WebMethod]
		public void xmlEmplInfoCo(int jobPos,string fieldName,string fieldValue){
			try{
				if( UOX.HasPageSessionID ) uox.LockApplicant();
				xmlEmplInfo(1,jobPos,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException("Field:"+fieldName+",Value:"+fieldValue+"\n",exp);}
		}
		[WebMethod]
		public void xmlEmplInfo(int borrPos,int jobPos,string fieldName,string fieldValue){
			try{
				if( UOX.HasPageSessionID ) uox.LockApplicant();
				SQL.EmplInfo(uox.db,uox.ApplInfo.BorrowerID(borrPos),0,jobPos,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException("Field:"+fieldName+",Value:"+fieldValue+"\n",exp);}
		}
		#endregion
		
		#region Address Info
		[WebMethod]
		public void xmlAddrInfo_0(string fieldName,string fieldValue){
			try{
				XmlAddrInfo(0,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException(exp);}
		}
		[WebMethod]
		public void xmlAddrInfo_1(string fieldName,string fieldValue){
			try{
				XmlAddrInfo(1,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException(exp);}
		}
		[WebMethod]
		public int xmlAddrInfo(int index,string fieldName,object fieldValue){
			try{
				return XmlAddrInfo(index,fieldName,fieldValue);
			}catch(Exception exp){ throw new ClientException(exp);}
		}
		public int XmlAddrInfo(int index,string fieldName,object fieldValue){
			try{
				if( UOX.HasPageSessionID ) uox.LockApplicant();
				string sql = " FROM t_Address WHERE BorrowerID=@BID AND Pos=@Pos";
				int borrID = uox.ApplInfo.BorrowerID(0);
				HashTableEx ht = new HashTableEx(fieldName,fieldValue,"BorrowerID",borrID,"Pos",index,"AddressTypeID",1);
				SQL.UpdateDataTable(ht,db,"SELECT ID,BorrowerID,Pos,AddressTypeID,"+fieldName+sql,borrID,index);
				return (int)SQL.ExecuteScalarEx(db,"SELECT ID"+sql,borrID,index);
			}catch(Exception exp){ throw new ClientException(exp);}
}
		#endregion
	}
}
